package com.zhongyu.controller;

import com.zhongyu.pojo.GoodsType;
import com.zhongyu.pojo.Org;
import com.zhongyu.service.GoodsTypeService;
import com.zhongyu.vo.GoodsTypeVo;
import com.zhongyu.vo.GoodsVo;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
/*
商品类型
 */
@RestController
@RequestMapping("/jibenxinxi/splx")
public class GoodsTypeController {
    @Resource
    private GoodsTypeService goodsTypeService;
    @GetMapping("/search/{pageNo}/{pageSize}")
    public ResultVo search(@PathVariable Integer pageNo, @PathVariable Integer pageSize, GoodsTypeVo goodsTypeVo){
        PageResult pageResult=goodsTypeService.searchAll(pageNo,pageSize,goodsTypeVo);
        return ResultVo.success(pageResult);

    }
    @GetMapping("/list")
    public ResultVo list() {
        List<GoodsType> list = goodsTypeService.searchAll();
        return ResultVo.success(list);

    }
    @PostMapping("/update")
    @RequiresPermissions("splx:update")
    public ResultVo update(@RequestBody GoodsType goodsType, HttpSession session) {
        try {
            if ((goodsTypeService.searchByName(goodsType.getGoodsTpname()).size()>0)){
                return ResultVo.success("该商品类型未修改或已存在!");
            }
            goodsTypeService.update(goodsType);
            return ResultVo.success("修改商品类型成功!");
        }catch (Exception ex){
            return ResultVo.failure("修改商品类型失败!",ex);
        }
    }

    @GetMapping("/del")
    @RequiresPermissions("splx:del")
    public ResultVo del(Integer id) {
        try {
            goodsTypeService.del(id);
            return ResultVo.success("删除商品类型成功!");
        } catch (Exception ex) {

            return ResultVo.failure("删除商品类型失败！", ex);
        }
    }
    @PostMapping("/add")
    @RequiresPermissions("splx:add")
    public ResultVo add(@RequestBody GoodsType goodsType, HttpSession session){
        try{
            if (goodsTypeService.searchByName(goodsType.getGoodsTpname()).size()>0){
                return ResultVo.success("该商品类型已存在!");
            }
            goodsTypeService.add(goodsType);
            return ResultVo.success("添加商品类型成功!");
        }catch (Exception ex){
            return ResultVo.failure("添加商品类型失败!",ex);
        }
    }
    }
